<?php

class Present extends Connect{
        
    function getAll(){
        return $this->getPresent(false);
    }
    
    function getPresent($count = 1){
                        
        $sql = "SELECT * FROM `".PREFIX_STATIC."presents` ORDER BY RAND() ";
        $sql .= ($count) ? "LIMIT ".$count : "" ;
        if(!$result = DBConnect::query($sql, 'presents')){
            $this->error(mysql_error());
            return false;
        }
        DBConnect::getAll($result, 'presents');
        return DBConnect::$all;        
    }
    
    function setPresent($type, $id){
        $sql = "SELECT `id` FROM  `".PREFIX_STATIC."presents` WHERE `type` = '".$type."' AND `good` = '".$id."' LIMIT 1 ";
        if(!$result = DBConnect::query($sql)){
            $this->error(mysql_error());
            return false;
        }
        if(DBConnect::mysqlCount($result) > 0) return false;
        
        $sql = "INSERT INTO `".PREFIX_STATIC."presents` (`type`, `good` ) VALUES ('".$type."', '".$id."') ";
        if(!$result = DBConnect::query($sql)){
            $this->error(mysql_error());
            return false;
        }
        return mysql_insert_id();
    } 
    
    function deletePresent($id){
        $sql = "DELETE FROM `".PREFIX_STATIC."presents` WHERE `id` = '".$id."' LIMIT 1";
        if(!$result = DBConnect::query($sql)){
            $this->error(mysql_error());
            return false;
        }
        return true;
    }
    
}

?>